package dao

import (
	"goblog/models"
	"github.com/astaxie/beego/orm"
)

type UserDao struct {
	Dao orm.Ormer
}

func (this *UserDao) Get(id int) *models.User {
	if this.Dao == nil {
		this.Dao = orm.NewOrm()
	}
	var user models.User
	this.Dao.Raw("select id, username, password from users where id=?", id).QueryRow(&user)
	if user.Id > 0 {
		return &user
	}
	return nil
}

func (this *UserDao) GetByName(name string) *models.User {
	if this.Dao == nil {
		this.Dao = orm.NewOrm()
	}
	var user models.User
	this.Dao.Raw("select id, username, password from users where username=?", name).QueryRow(&user)
	if user.Id > 0 {
		return &user
	}
	return nil
}
